package com.warm.pump.module.work.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

import com.warm.pump.frame.gen.bean.PageBean;
import com.warm.pump.module.work.bean.po.gen.WorkSubject;
import com.warm.pump.module.work.service.IWorkSubjectService;
import com.warm.pump.frame.gen.util.OutJson;
import com.warm.pump.frame.gen.util.MyBeanUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * work_subject
 */
@Controller
@RequestMapping("/workSubject")
public class WorkSubjectController {
    @Autowired
    private IWorkSubjectService workSubjectService;

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("workSubject:list")
    public OutJson list(Integer page, Integer limit, WorkSubject workSubject, String subjectNameLike, Long jobId) {
        Map<String, Object> map = new HashMap<>();
        MyBeanUtils.copyBean2Map(map, workSubject);

        if (StringUtils.isNotEmpty(subjectNameLike)) {
            map.put("subjectName_like", subjectNameLike);
        }
        if (StringUtils.isNotEmpty(workSubject.getSidx()) && StringUtils.isNotEmpty(workSubject.getOrder())) {
            map.put("orderBy", workSubject.getSidx() + " " + workSubject.getOrder());
        }

        //查询列表数据
        PageBean pageBean = workSubjectService.getPageBeanByMap(map, page, limit, jobId);

        return OutJson.ok().put("page", pageBean);
    }


    /**
     * 信息
     */
    @ResponseBody
    @RequestMapping("/info/{id}")
    @RequiresPermissions("workSubject:info")
    public OutJson info(@PathVariable("id") Long id) {
        WorkSubject workSubject = workSubjectService.queryObject(id);

        return OutJson.ok().put("workSubject", workSubject);
    }


    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping("/save")
    @RequiresPermissions("workSubject:save")
    public OutJson save(@RequestBody WorkSubject workSubject) {
        workSubject.setCreateTime(new Date());
        workSubject.setUpdateTime(new Date());
        workSubjectService.save(workSubject);

        return OutJson.ok();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("workSubject:update")
    public OutJson update(@RequestBody WorkSubject workSubject) {
        workSubject.setUpdateTime(new Date());
        workSubjectService.update(workSubject);

        return OutJson.ok();
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("workSubject:delete")
    public OutJson delete(@RequestBody Long[] ids) {
        workSubjectService.deleteBatch(ids);

        return OutJson.ok();
    }

    /**
     * 立即执行任务
     */
    @ResponseBody
    @RequestMapping("/run")
    public OutJson run(@RequestBody Long[] ids) {
        workSubjectService.run(ids);
        return OutJson.ok();
    }
}
